import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'dateText'
})
export class DateTextPipe implements PipeTransform {

  transform(value: any, ...args: any[]): any {

    if (typeof value == 'string') {
      value = parseInt(value + '')
    }

    const date = new Date(value)

    const now = new Date()

    if (date.getFullYear() != now.getFullYear()) {
      return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日'
    }

    if (date.getMonth() != now.getMonth()) {
      return (date.getMonth() + 1) + '月' + date.getDate() + '日'
    }

    if (date.getDate() <= now.getDate() - 2) {
      return (date.getMonth() + 1) + '月' + date.getDate() + '日'
    }

    if (date.getDate() == now.getDate() - 1) {
      return '昨天'
    }

    if (date.getHours() < 12) {
      return '上午' + date.getHours() + ':' + date.getMinutes()
    }

    if (date.getHours() >= 13) {
      return '下午' + (date.getHours() - 12) + ':' + date.getMinutes()
    }

    return '中午' + date.getHours() + ':' + date.getMinutes()
  }
}
